import Vue from 'vue';

const state = {
  path:"/",
  newsList:[],
  newsAll:{},
  goodsList:[],
  photoList:[],
  filter:{
      rows: 3,
      page: 1,
      total: 0,
  },
};

const getters = {
  getPath:state => {
    return state.path;
  },
  getNewsList:state => {
    return state.newsList;
  },
  getNewsAll:state => {
    return state.newsAll;
  },
  getFilter:state => {
    return state.filter;
  },
  getGoodsList:state => {
    return state.goodsList;
  },
  getPhotoList:state => {
    return state.photoList;
  },
};

const mutations = {
    setPath:(state,payload) => {
      state.path = payload;
    },
    setNewsList:(state,payload) => {
      state.newsList = payload.rows;
      state.getNewsAll = payload;
    },
    setGoodsList:(state,payload) => {
      state.goodsList = payload.rows;
    },
    setPhoto:(state,payload) => {
      state.photoList = payload.rows;
    },
}

const actions = {
    setPath:({commit},payload)=>{
      commit("setPath",payload)
    },
    setNewsList:({commit},payload)=>{
      Vue.axios.post("http://localhost:3000/news/list",payload)
      .then(res => {
        commit("setNewsList",res.data)
      })
    },
    setGoodsList:({commit},payload)=>{
      Vue.axios.post("http://localhost:3000/goods/list",payload)
      .then(res => {
        commit("setGoodsList",res.data)
      })
    },
    setPhoto:({commit},payload)=>{
      Vue.axios.post("http://localhost:3000/upload/list")
      .then(res => {
        commit("setPhoto",res.data)
      })
    },
}

export default {
  state,
  getters,
  mutations,
  actions
}
